<template>
	<view class="container">
		<!-- 侧边导航栏 -->
		<view class="sidebar">
			<view v-for="(item, index) in navItems" :key="index"
				:class="['nav-item', { active: activeIndex === index }]" @click="handleNavClick(index, item.path)">
				{{ item.name }}
			</view>
		</view>
		<view class="content">


			<view class="search-bar">
				<view> 病历ID：</view>
				<input class="search-input" v-model="diagId" placeholder="请输入搜索内容" />
				<button class="search-button" @click="search">查询</button>
			</view>
			<view class="info-bar">
				<view class="info-label">当前信息：</view>
				<view class="info-table">
					<view class="info-row">
						<view class="info-cell">病人ID</view>
						<view class="info-cell">{{ patientId }}</view>
						<view class="info-cell">病人姓名</view>
						<view class="info-cell">{{ patientName }}</view>
						<view class="info-cell">性别</view>
						<view class="info-cell">{{ patientGender }}</view>
						<view class="info-cell">病人电话</view>
						<view class="info-cell">{{ patientTel }}</view>
					</view>
				</view>
			</view>
			<view class="table">
				<view class="table-header">
					<view class="table-cell">名称</view>
					<view class="table-cell">规格</view>
					<view class="table-cell">数量</view>
					<view class="table-cell">单价</view>
					<view class="table-cell">小计</view>
					<view class="table-cell">类别</view>
				</view>
				<view class="table-body">
					<view class="table-row" v-for="(item, index) in items" :key="index">
						<view class="table-cell">{{ item.name }}</view>
						<view class="table-cell">{{ item.spec }}</view>
						<view class="table-cell">{{ item.quantity }}</view>
						<view class="table-cell">{{ item.price }}</view>
						<view class="table-cell">{{ item.price * item.quantity }}</view>
						<view class="table-cell">{{ item.category }}</view>
					</view>
				</view>
			</view>
			<view class="total-amount">
				<view class="total-label">总金额：</view>
				<view class="total-value">{{ totalAmount }}</view>
				<button class="confirm-button" @click="confirmPayment">确认收费</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				patientId: '',
				patientName: '',
				patientGender: '',
				doctorName: '',
				patientTel:'',
				items: [],
				diagId: "",
				originalData: {
					patientId: '',
					patientName: '',
					patientGender: '',
					doctorName: '',
					items: []
				},
				// 导航栏项
				navItems: [{
						name: '处方管理',
						path: '/pages/Prescription/Prescription'
					},
					{
						name: '药品管理',
						path: '/pages/drugManagement/drugManagement'
					},
					{
						name: '收费管理',
						path: '/pages/Cachier/Cachier'
					},
					{
						name: '退费管理',
						path: '/pages/Cachier/Refund'
					}
				],
				// 当前激活的导航项索引
				activeIndex: 2,
				//链接相关
				baseUrl:"http://172.22.224.1:8080"
			};
		},
		computed: {
			totalAmount() {
				return this.items.reduce((total, item) => total + item.price * item.quantity, 0);
			}
		},
		methods: {
			async confirmPayment() {
				if (!this.diagId) {
					uni.showToast({
						title: '病历ID不能为空',
						icon: 'none'
					});
					return;
				}

				try {
					const response = await uni.request({
						url: this.baseUrl+'/cashier/chargeConfirmByDiagId', // 接口地址
						method: 'POST',
						data: {
							diagId: this.diagId
						},
						header: {
							'Content-Type': 'application/json'
						}
					});

					const data = response.data;

					if (data.code === 200) {
						uni.showToast({
							title: '收费确认成功',
							icon: 'success'
						});
						this.clearData(); // 清空页面数据
					} else {
						uni.showToast({
							title: data.message || '收费确认失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('请求失败:', error);
					uni.showToast({
						title: '请求失败，请重试',
						icon: 'none'
					});
				}
			},
			clearData() {
				// 清空页面数据
				this.patientId = '';
				this.patientName = '';
				this.patientGender = '';
				this.doctorName = '';
				this.diagId = '';
				this.items = [];
				this.patientTel='';
			},
			async search() {
				if (this.diagId === '') {
					uni.showToast({
						title: '请输入病历ID',
						icon: 'none'
					});
					return;
				}

				try {
					const response = await uni.request({
						url: this.baseUrl+'/cashier/getAllUnpaid', // 接口地址
						method: 'POST',
						data: {
							diagId: this.diagId
						},
						header: {
							'Content-Type': 'application/json'
						}
					});

					const data = response.data.data;
					console.log(data);

					if (data) {
						// 更新病历信息
						this.patientId = data.patient.id;
						this.patientName = data.patient.name;
						this.patientGender = data.patient.sex=0?"女":"男";
						this.patientTel=data.patient.tel;
						this.doctorName = data.docname;
						this.items = data.items;
						console.log("this.diagId="+this.diagId);
					} else {
						uni.showToast({
							title: '未找到相关病历信息',
							icon: 'none'
						});
						this.resetData();
					}
				} catch (error) {
					console.error('请求失败:', error);
					uni.showToast({
						title: '请求失败，请重试',
						icon: 'none'
					});
					this.resetData();
				}
			},
			resetData() {
				// 重置数据到初始状态
				this.patientId = this.originalData.patientId;
				this.patientName = this.originalData.patientName;
				this.patientGender = this.originalData.patientGender;
				this.doctorName = this.originalData.doctorName;
				this.items = this.originalData.items;
			},
			// 处理导航项点击事件
			handleNavClick(index, path) {
				this.activeIndex = index;
				// 跳转到对应页面
				uni.redirectTo({
					url: path
				});
			}
		}
	};
</script>

<style>
	.container {
		display: flex;
		height: 100vh;
	}

	.sidebar {
		width: 200px;
		background-color: #f0f0f0;
		padding: 20px;
	}

	.nav-item {
		padding: 10px;
		margin-bottom: 10px;
		cursor: pointer;
		border-radius: 5px;
		transition: background-color 0.3s;
	}

	.nav-item.active {
		background-color: #007aff;
		color: white;
	}

	.nav-item:hover {
		background-color: #e0e0e0;
	}

	.content {
		flex: 1;
		padding: 20px;
		background-color: #fff;
	}

	.info-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.info-label {

		font-weight: bold;

		margin-right: 20px;

	}

	.info-table {
		border-collapse: collapse;
		width: 100%;
	}

	.info-row {
		display: flex;
	}

	.info-cell {
		flex: 1;
		padding: 10px;
		border: 1px solid #ddd;
	}

	.table {
		border-collapse: collapse;
		width: 100%;
	}

	.table-header {
		display: flex;
		background-color: #f5f5f5;
	}

	.table-cell {
		flex: 1;
		padding: 10px;
		border: 1px solid #ddd;
	}

	.table-body {
		display: flex;
		flex-direction: column;
	}

	.table-row {
		display: flex;
	}

	.table-row:nth-child(odd) {
		background-color: #f9f9f9;
	}

	.total-amount {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20px;
	}

	.total-label {
		font-weight: bold;
	}

	.confirm-button {
		padding: 10px 20px;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}

	.confirm-button:hover {
		background-color: #0056b3;
	}

	.search-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.search-select {
		padding: 10px;
		border: 1px solid #ddd;
		border-radius: 5px;
		margin-right: 10px;
	}

	.search-input {
		flex: 1;
		padding: 10px;
		border: 1px solid #ddd;
		border-radius: 5px;
		margin-right: 10px;
	}

	.search-button {
		padding: 10px 20px;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 5px;
		cursor: pointer;
	}

	.search-button:hover {
		background-color: #0056b3;
	}
</style>